/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import controller.ControllerFornecedor;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.PersistenceException;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import model.fornecedor.Fornecedor;
import model.excecoes.ErroDAOException;
import model.excecoes.ErroNegocioException;

/**
 *
 * @author aluno
 */
public class FormFornecedor extends javax.swing.JFrame {

    /**
     * Creates new form FormFornecedor
     */
    private Fornecedor fornecedor = null;
    private ControllerFornecedor controlFornecedor = null;
    private int linhaSelecionada = 0;
    private List<Fornecedor> fornecedores = null;
    private DefaultTableModel model = null;

    public FormFornecedor() {
        initComponents();
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FormFornecedor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FormFornecedor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FormFornecedor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FormFornecedor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        controlFornecedor = new ControllerFornecedor();
        this.setVisible(true);
        this.setLocationRelativeTo(null);
        model = new DefaultTableModel();
        model.setColumnIdentifiers(new String[]{"Código", "Razão Social", "Rua", "CNPJ"});
    }

    private void preencher(String operacao) {
        if (operacao.equals("Cadastrar")) {
            fornecedor = new Fornecedor();
            fornecedor.setRazaoSocial(jTRazaoSocial.getText());
            fornecedor.getEndereco().setRua(jTRua.getText());
            fornecedor.getEndereco().setCidade(jTCidade.getText());
            fornecedor.getEndereco().setBairro(jTBairro.getText());
            fornecedor.getEndereco().setCep(jFCEP.getText());
            fornecedor.getEndereco().setComplemento(jTComplemento.getText());
            fornecedor.getEndereco().setUf(comboUF.getSelectedItem().toString());
            fornecedor.setCnpj(jTCNPJ.getText());
            fornecedor.getContato().setTelefone(jFTelefone.getText());
            fornecedor.getContato().setCelular(jFCelular.getText());
            fornecedor.getContato().setEmail(jTEMail.getText());
        } else {
            if (operacao.equals("Alterar")) {
                linhaSelecionada = jTable2.getSelectedRow();
                jTRazaoSocial.setText(fornecedores.get(linhaSelecionada).getRazaoSocial());
                jTRua.setText(fornecedores.get(linhaSelecionada).getEndereco().getRua());
                jTCidade.setText(fornecedores.get(linhaSelecionada).getEndereco().getCidade());
                jTBairro.setText(fornecedores.get(linhaSelecionada).getEndereco().getBairro());
                jFCEP.setText(fornecedores.get(linhaSelecionada).getEndereco().getCep());
                jTComplemento.setText(fornecedores.get(linhaSelecionada).getEndereco().getComplemento());
                DefaultComboBoxModel model = new DefaultComboBoxModel();
                model.addElement(fornecedores.get(linhaSelecionada).getEndereco().getUf());
                comboUF.setModel(model);
                jTCNPJ.setText(fornecedores.get(linhaSelecionada).getCnpj());
                jFTelefone.setText(fornecedores.get(linhaSelecionada).getContato().getTelefone());
                jFCelular.setText(fornecedores.get(linhaSelecionada).getContato().getCelular());
                jTEMail.setText(fornecedores.get(linhaSelecionada).getContato().getEmail());
                fornecedor = new Fornecedor();
                fornecedor.setCodigo(fornecedores.get(linhaSelecionada).getCodigo());
                fornecedor.setRazaoSocial(jTRazaoSocial.getText());
                fornecedor.getEndereco().setRua(jTRua.getText());
                fornecedor.getEndereco().setCidade(jTCidade.getText());
                fornecedor.getEndereco().setBairro(jTBairro.getText());
                fornecedor.getEndereco().setCep(jFCEP.getText());
                fornecedor.getEndereco().setComplemento(jTComplemento.getText());
                fornecedor.getEndereco().setUf(comboUF.getSelectedItem().toString());
                fornecedor.setCnpj(jTCNPJ.getText());
                fornecedor.getContato().setTelefone(jFTelefone.getText());
                fornecedor.getContato().setCelular(jFCelular.getText());
                fornecedor.getContato().setEmail(jTEMail.getText());
            } else {
                fornecedor = new Fornecedor();
                fornecedor.setCodigo(fornecedores.get(linhaSelecionada).getCodigo());

            }
        }
    }

    
    private void listar(String op){
        try {
            if(op.equals("Todos")){
                fornecedores = controlFornecedor.listarFornecedor("Todos", "Todos");
            }else{
                if(op.equals("Código")){
                    fornecedores = controlFornecedor.listarFornecedor(jTConsulta.getText(), "Código");
                }else{
                    if(op.equals("Razão Social")){
                        fornecedores = controlFornecedor.listarFornecedor(jTConsulta.getText(), "Razão Social");
                    }else{
                        if(op.equals("CNPJ")){
                            fornecedores = controlFornecedor.listarFornecedor(jTConsulta.getText(), "CNPJ");
                        }
                    }
                }
            }
            
            for(Fornecedor f: fornecedores){
                model.addRow(new String[]{String.valueOf(f.getCodigo()), f.getRazaoSocial(), f.getEndereco().getRua(), f.getCnpj(), f.getContato().getTelefone(), f.getContato().getCelular(), f.getContato().getEmail()});
            }
            jTable2.setModel(model);
        } catch (ErroDAOException ex) {
            Logger.getLogger(FormFornecedor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (PersistenceException ex) {
            Logger.getLogger(FormFornecedor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ErroNegocioException ex) {
            Logger.getLogger(FormFornecedor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTRazaoSocial = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTRua = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTCidade = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTBairro = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jFCEP = new javax.swing.JFormattedTextField();
        jLabel6 = new javax.swing.JLabel();
        jTComplemento = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        comboUF = new javax.swing.JComboBox();
        jLabel13 = new javax.swing.JLabel();
        jTCNPJ = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        jFTelefone = new javax.swing.JFormattedTextField();
        jLabel9 = new javax.swing.JLabel();
        jFCelular = new javax.swing.JFormattedTextField();
        jLabel10 = new javax.swing.JLabel();
        jTEMail = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jLabel11 = new javax.swing.JLabel();
        jTConsulta = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        comboCampo = new javax.swing.JComboBox();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel14 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel16 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jButton6 = new javax.swing.JButton();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Dados Cadastrais do Fornecedor"));

        jLabel1.setText("Razão Social");

        jLabel2.setText("Rua");

        jLabel3.setText("Cidade");

        jLabel4.setText("Bairro");

        jLabel5.setText("CEP");

        jLabel6.setText("Complemento");

        jLabel7.setText("UF");

        comboUF.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Acre", "Alagoas", "Amazonas", "Amapá", "Bahia", "Ceará", "Distrito Federal", "Espírito Santo", "Goiás", "Maranhão", "Mato Grosso", "Mato Grosso do Sul", "Minas Gerais", "Pará", "Paraná", "Pernambuco", "Piauí", "Rio Grande do Norte", "Rio Grande do Sul", "Rio de Janeiro", "Rondônia", "Roraima", "Santa Catarina", "São Paulo", "Sergipe", "Tocantins" }));

        jLabel13.setText("CNPJ");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTRazaoSocial)
                    .addComponent(jTRua)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jTCidade, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTBairro, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jFCEP))
                    .addComponent(jTComplemento)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(comboUF, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel13)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTCNPJ, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTRazaoSocial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTRua, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTCidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(jTBairro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(jFCEP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jTComplemento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(comboUF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel13)
                    .addComponent(jTCNPJ, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Contato com o Fornecedor"));

        jLabel8.setText("Telefone");

        jLabel9.setText("Celular");

        jLabel10.setText("E-Mail");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jFTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jFCelular, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jTEMail))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jFTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(jFCelular, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(jTEMail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/7893_64x64.png"))); // NOI18N
        jButton1.setText("Cadastrar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/8448_128x128.png"))); // NOI18N
        jButton2.setText("Alterar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/846_128x128.png"))); // NOI18N
        jButton3.setText("Excluir");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "Código", "Razão Social", "Rua", "CNPJ", "Telefone", "Celular", "E-Mail"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Object.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane2.setViewportView(jTable2);

        jLabel11.setText("Consulta");

        jLabel12.setText("Campo");

        comboCampo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Todos", "Código", "Razão Social", "CNPJ" }));

        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/7833_64x64.png"))); // NOI18N
        jButton4.setText("Listar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/8443_128x128.png"))); // NOI18N
        jButton5.setText("Fechar");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Dados do Produto"));

        jLabel14.setText("Descrição do Produto");

        jLabel15.setText("Marca do Produto");

        jLabel16.setText("Preço do Produto");

        jTextField3.setBackground(new java.awt.Color(255, 255, 204));
        jTextField3.setFocusable(false);

        jButton6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/8440_128x128.png"))); // NOI18N
        jButton6.setText("Adicionar Produto");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel14)
                    .addComponent(jLabel15)
                    .addComponent(jLabel16))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextField1)
                            .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 231, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton6))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton6)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel14)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel15))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel16)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(42, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton5))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton3))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel11)
                                .addGap(130, 130, 130)
                                .addComponent(jLabel12))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jTConsulta, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(comboCampo, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton4)))
                        .addGap(0, 639, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton1, jButton2, jButton3});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jButton3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(jLabel12))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTConsulta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(comboCampo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton4))
                .addGap(12, 12, 12)
                .addComponent(jButton5)
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jButton1, jButton2, jButton3});

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        linhaSelecionada = jTable2.getSelectedRow();
        if (linhaSelecionada == -1) {
            JOptionPane.showMessageDialog(rootPane, "Para alterar algum dado do Fornecedor por favor clique em alguma linha da tabela.", "Atenção", JOptionPane.WARNING_MESSAGE);
        } else {
            if (ValidarConteudo.validar(jTRazaoSocial, "Razão Social") == false) {
                jTRazaoSocial.requestFocus();
            } else {
                if (ValidarConteudo.validar(jTRua, "Rua") == false) {
                    jTRua.requestFocus();
                } else {
                    if (ValidarConteudo.validar(jTBairro, "Bairro") == false) {
                        jTBairro.requestFocus();
                    } else {
                        if (ValidarConteudo.validar(jFCEP, "CEP") == false) {
                            jFCEP.requestFocus();
                        } else {
                            if (ValidarConteudo.validar(jTComplemento, "Complemento") == false) {
                                jTComplemento.requestFocus();
                            } else {
                                if (ValidarConteudo.validar(jTCNPJ, "CNPJ") == false) {
                                    jTCNPJ.requestFocus();
                                } else {
                                    if (ValidarConteudo.validar(jFTelefone, "Telefone") == false) {
                                        jFTelefone.requestFocus();
                                    } else {
                                        if (ValidarConteudo.validar(jFCelular, "Celular") == false) {
                                            jFCelular.requestFocus();
                                        } else {
                                            if (ValidarConteudo.validar(jTEMail, "E-Mail") == false) {
                                                jTEMail.requestFocus();
                                            } else {
                                                preencher("Alterar");
                                                try {
                                                    controlFornecedor.alterarFornecedor(fornecedor);
                                                    JOptionPane.showMessageDialog(rootPane, "Os dados do Fornecedor foram alterados com sucesso!", "Operação realizada com sucesso!", JOptionPane.INFORMATION_MESSAGE);
                                                    this.dispose();
                                                } catch (ErroDAOException ex) {
                                                    JOptionPane.showMessageDialog(rootPane, ex.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
                                                } catch (PersistenceException ex) {
                                                    JOptionPane.showMessageDialog(rootPane, ex.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
                                                } catch (ErroNegocioException ex) {
                                                    JOptionPane.showMessageDialog(rootPane, ex.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        if (ValidarConteudo.validar(jTRazaoSocial, "Razão Social") == false) {
            jTRazaoSocial.requestFocus();
        } else {
            if (ValidarConteudo.validar(jTRua, "Rua") == false) {
                jTRua.requestFocus();
            } else {
                if (ValidarConteudo.validar(jTBairro, "Bairro") == false) {
                    jTBairro.requestFocus();
                } else {
                    if (ValidarConteudo.validar(jFCEP, "CEP") == false) {
                        jFCEP.requestFocus();
                    } else {
                        if (ValidarConteudo.validar(jTComplemento, "Complemento") == false) {
                            jTComplemento.requestFocus();
                        } else {
                            if (ValidarConteudo.validar(jTCNPJ, "CNPJ") == false) {
                                jTCNPJ.requestFocus();
                            } else {
                                if (ValidarConteudo.validar(jFTelefone, "Telefone") == false) {
                                    jFTelefone.requestFocus();
                                } else {
                                    if (ValidarConteudo.validar(jFCelular, "Celular") == false) {
                                        jFCelular.requestFocus();
                                    } else {
                                        if (ValidarConteudo.validar(jTEMail, "E-Mail") == false) {
                                            jTEMail.requestFocus();
                                        } else {
                                            preencher("Cadastrar");
                                            try {
                                                controlFornecedor.cadastrarFornecedor(fornecedor);
                                                JOptionPane.showMessageDialog(rootPane, "Os dados do Fornecedor foram cadastrados com sucesso!", "Operação realizada com sucesso!", JOptionPane.INFORMATION_MESSAGE);
                                                this.dispose();
                                            } catch (ErroDAOException ex) {
                                                JOptionPane.showMessageDialog(rootPane, ex.getMessage(), "Erro de DAO", JOptionPane.ERROR_MESSAGE);
                                            } catch (PersistenceException ex) {
                                                JOptionPane.showMessageDialog(rootPane, ex.getMessage(), "Erro de Persistencia", JOptionPane.ERROR_MESSAGE);
                                            } catch (ErroNegocioException ex) {
                                                JOptionPane.showMessageDialog(rootPane, ex.getMessage(), "Erro de Sistema", JOptionPane.ERROR_MESSAGE);
                                            }

                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        linhaSelecionada = jTable2.getSelectedRow();
        if (linhaSelecionada == -1) {
            JOptionPane.showMessageDialog(rootPane, "Para excluir algum registro de Fornecedor por favor clique em alguma linha na tabela e selecione o registro procurado ou cancele a operação!");
        } else {

            Object[] botoes = {"Sim", "Não"};
            int opcao = JOptionPane.showInternalOptionDialog(rootPane, "Deseja excluir realmente o registro?", "Confirmar!",
                    JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, botoes, botoes[0]);

            if (opcao == JOptionPane.YES_OPTION) {

                try {
                    preencher("Excluir");
                    controlFornecedor.excluirFornecedor(fornecedor);
                    JOptionPane.showMessageDialog(rootPane, "Os dados do Fornecedor foram excluídos com sucesso!", "Atenção!", JOptionPane.INFORMATION_MESSAGE);
                    this.dispose();
                } catch (ErroDAOException ex) {
                    JOptionPane.showMessageDialog(rootPane, ex.getMessage(), "Erro de DAO", JOptionPane.ERROR_MESSAGE);
                } catch (PersistenceException ex) {
                    JOptionPane.showMessageDialog(rootPane, ex.getMessage(), "Erro de Sistema", JOptionPane.ERROR_MESSAGE);
                } catch (ErroNegocioException ex) {
                    JOptionPane.showMessageDialog(rootPane, ex.getMessage(), "Erro de Sistema", JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        listar(comboCampo.getSelectedItem().toString());
    }//GEN-LAST:event_jButton4ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox comboCampo;
    private javax.swing.JComboBox comboUF;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JFormattedTextField jFCEP;
    private javax.swing.JFormattedTextField jFCelular;
    private javax.swing.JFormattedTextField jFTelefone;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jTBairro;
    private javax.swing.JTextField jTCNPJ;
    private javax.swing.JTextField jTCidade;
    private javax.swing.JTextField jTComplemento;
    private javax.swing.JTextField jTConsulta;
    private javax.swing.JTextField jTEMail;
    private javax.swing.JTextField jTRazaoSocial;
    private javax.swing.JTextField jTRua;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    // End of variables declaration//GEN-END:variables
}
